package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.CategoryService;
import com.qf.service.brandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DtsBrandVo;
import com.sun.org.apache.bcel.internal.generic.DDIV;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 11:10
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {

     @Autowired
      private brandService brandService;

    @Autowired
     private CategoryService categoryService;

     @Autowired
     private AdminService service;
    @RequestMapping("/list")
    public  Object brand(Integer id,String name,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit,
                         @RequestParam(defaultValue = "add_time") String sort,
                         @RequestParam(defaultValue = "desc") String order){
        IPage<DtsBrand> list = brandService.list(id, name, page, limit, sort, order);
        List<DtsBrand> records = list.getRecords();
        long total = list.getTotal();
        HashMap<String,Object> data= new HashMap<>();
        data.put("total", total);
        data.put("items", records);

        return ResponseUtil.ok(data);
    }

    /**
     * 下拉框
     * @return
     */
    @GetMapping("/catAndAdmin")
    public  Object catAndAdmin(){
        //获取所有管理信息
        List<DtsAdmin> adminByall = service.findAdminByall();
        //把管理员信息封装到实体类中Catvo中
        List<CatVo> adminList = vo(adminByall);
        //根据sql语句查询自动封装到实体类中对应 value=id  leable=name
        List<CatVo> list = brandService.list();
        //获取表中分类表所有信息
        List<DtsCategory> listall = categoryService.list();
       //调用封装，把list集合中Catvo实体类中的list集合补全
        List<CatVo> categoryList = queryList(list, listall);
       //
        HashMap<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList", categoryList);
        return  ResponseUtil.ok(data);
    }

    /**
     * 添加
     * @param brand
     * @return
     */
   @PostMapping("/create")
    public  Object create(@RequestBody DtsBrand brand){

       DtsBrand add = brandService.add(brand);

       return ResponseUtil.ok(add);
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @RequestMapping("/update")
    public  Object update(@RequestBody DtsBrand brand){
        int update = brandService.update(brand);
        if(update==0){
            throw  new RuntimeException("修改失败");
        }

        return  ResponseUtil.ok();

    }

    /**
     * 删除
     * @param dtsBrand
     * @return
     */
    @PostMapping("/delete")
    public  Object deleted(@RequestBody DtsBrand dtsBrand){
        int deleted = brandService.deleted(dtsBrand);
        return ResponseUtil.ok();
    }
    /**
     * 封装Catvo类中的集合类型
     * @param list
     * @param listall
     * @return
     */
      public List<CatVo> queryList( List<CatVo> list,  List<DtsCategory> listall){
        if(list==null || listall==null){
          throw  new RuntimeException("数据为空");
        }

          for (int i = 0; i < list.size(); i++) {
              //创建一个类的集合里面装实体类
              List<CatVo> arrayList = new ArrayList<>();

              for (int j = 0; j < listall.size(); j++) {
                  //new 一个实体类用来存值
                  CatVo catVo = new CatVo();

                  if(list.get(i).getValue().equals(listall.get(j).getPid())){

                      Integer id = listall.get(j).getId();

                      String name = listall.get(j).getName();
                      //放入实体类
                      catVo.setValue(id);

                      catVo.setLabel(name);
                      //把实体类放入集合
                    arrayList.add(catVo);
                  }

              }
               //把list集合属性set值
              list.get(i).setChildren(arrayList);
          }


          return list;
    }


    /**
     * 封装admin
     * @param admins
     * @return
     */
     private List<CatVo> vo(List<DtsAdmin> admins){
         //
        List<CatVo> list = new ArrayList<CatVo>();

         for(int i = 0; i < admins.size(); i++) {

             CatVo catVo = new CatVo();

             Integer id = admins.get(i).getId();

             String username = admins.get(i).getUsername();

             catVo.setValue(id);

             catVo.setLabel(username);

             list.add(catVo);
         }
          return list;

     }
    }